package com.egg.demo.rabbitmq.listener;

import com.egg.auth.constants.AuthRabbitExchange;
import com.egg.common.core.constant.StringPool;
import com.egg.common.core.constant.rabbit.RabbitRoutingKey;
import com.egg.common.util.JsonUtil;
import com.egg.demo.constants.DemoConstant;
import com.egg.demo.constants.DemoRabbitExchange;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


/**
 * 监听
 *
 * @author east
 */
@Slf4j
@Component
public class DemoListener {


    /**
     * 监听-示例
     * 本队列 = 本系统 + 生产者渠道 + 生产者KEY
     *
     * @param message
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(name = DemoConstant.SYSTEM_NAME + StringPool.DOT + DemoRabbitExchange.DEMO_EXCHANGE + StringPool.DOT + RabbitRoutingKey.ADD)
                    , exchange = @Exchange(name = AuthRabbitExchange.DEMO_EXCHANGE)
                    , key = {RabbitRoutingKey.ADD}
            )
    })
    @Transactional(rollbackFor = Throwable.class)
    public void listener(Message message, Channel channel) {
        String messageId = message.getMessageProperties().getMessageId();
        log.debug("RabbitMQ messageId {} 响应准备 参数 {} ", messageId, JsonUtil.listToJson(message.getBody()));
        // TODO: 业务逻辑
        log.debug("RabbitMQ messageId {} 响应完成", messageId);
    }


}
